const { contextBridge, ipcRenderer, BrowserWindow } = require("electron");

ipcRenderer.on("SET_SOURCE", async (event, sourceId) => {
  try {
    const stream = await navigator.mediaDevices.getUserMedia({
      audio: false,
      video: {
        mandatory: {
          chromeMediaSource: "desktop",
          chromeMediaSourceId: sourceId,
          minWidth: 1280,
          maxWidth: 1280,
          minHeight: 720,
          maxHeight: 720,
        },
      },
    });
    console.log(stream);
  } catch (err) {
    console.log(err);
  }
});

contextBridge.exposeInMainWorld("bridgeApi", {
  closeApp() {
    ipcRenderer.send("close", "");
  },
  setStore(key, value) {
    // ElectronStore.set(key, value)
    ipcRenderer.sendSync("electron-store-set", key, value);
  },
  getStore(key) {
    // return ElectronStore.get(key)
    return ipcRenderer.sendSync("electron-store-get", key);
  },
  removeStore(key) {
    // ElectronStore.delete(key)
    ipcRenderer.sendSync("electron-store-remove", key);
  },
  clearStore() {
    // ElectronStore.clear()
    ipcRenderer.sendSync("electron-store-clear");
  },
  openWithDefaultBrowser(url) {
    ipcRenderer.send("open-url", url);
  },
  openWithNewWindow(url) {
    ipcRenderer.send("new-win", url);
  },
  getFile(cb) {
    ipcRenderer.send("get-path", "openFile");
    ipcRenderer.once("recive-path", (e, res) => {
      console.log(e, res);
      cb(res);
    });
  },
  getDirectory(cb) {
    ipcRenderer.send("get-path", "openDirectory");
    ipcRenderer.once("recive-path", (e, res) => {
      console.log(e, res);
      cb(res);
    });
  },
  fullScreen(flag) {
    console.log(flag);
    ipcRenderer.send("full-screen", flag);
  },
  miniApp() {
    ipcRenderer.send("mini-app");
  },
  openDevTools() {
    ipcRenderer.send("open-dev-tools");
  },
  closeDevTools() {
    ipcRenderer.send("close-dev-tools");
  },
  logBridge() {
    console.log(BrowserWindow);
  },
  gitPull(item) {
    console.log(item);
    ipcRenderer.send("git-pull", item);
  },
});
